package com.tilaiedu.mvc.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @Author xlzhang
 * @Date 2022/5/25 9:31
 * @Version 1.0
 * @Description TODO
 */
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        // 加载根容器的配置类
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // 加载mvc配置类，初始化SpringMVC容器
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // 配置dispatchServlet的拦截规则
        //  /拦截所有，但是jsp默认放行    /* 所有包括jsp
        return new String[]{"/"};
    }

    @Override
    protected Filter[] getServletFilters() {
        // 设置请求的字符编码，不设置时请求参数中有中文会乱码
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("UTF-8");
        // 创建一个隐藏域过滤器
        HiddenHttpMethodFilter httpMethodFilter = new HiddenHttpMethodFilter();

        return new Filter[]{encodingFilter,httpMethodFilter};
    }
}
